package com.y3k.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.y3k.service.ServiceException;
import com.y3k.utils.ConfigurationUtil;
import com.y3k.utils.ConnectionHelper;
import com.y3k.utils.DAOException;

public class ProfileDao {

	public static final Logger log = Logger.getLogger(ProfileDao.class);

	public List<String> findAllBy(String username) throws ServiceException {
		List<String> profiles = new ArrayList<String>();
		Connection conn = null;

		try {
			conn = ConnectionHelper.getConnection();

			String sql = ConfigurationUtil.CONFIGURATION.getProfileQuery();
			if (sql.toUpperCase().contains("WHERE")) {
				sql = sql + " and username=?";
			} else {
				sql = sql + " where username=?";
			}

			log.info(sql);
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, username);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				profiles.add(rs.getString("profile"));
			}

			conn.commit();
		} catch (Exception ex) {
			throw new DAOException(ex);
		}
		return profiles;
	}
}